package com.sos.widget.binding;

import android.os.Build;
import android.text.TextUtils;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.databinding.BindingAdapter;

public class WebViewBinding {
    @BindingAdapter(value = {"saas_webview_data"})
    public static void setWebData(WebView webView, String data) {
        if (webView != null) {
            webView.setBackgroundColor(0);
            WebSettings settings = webView.getSettings();
            settings.setJavaScriptEnabled(true);
            ////提高渲染的优先级
            settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
            //将图片调整到适合webview的大小
            settings.setUseWideViewPort(true);
            settings.setLoadWithOverviewMode(true);
            //不支持缩放
            settings.setSupportZoom(false);
            //隐藏原生缩放
            settings.setDisplayZoomControls(true);
            ////支持自动加载图片
//            settings.setTextZoom(200);
            ;
            settings.setLoadsImagesAutomatically(true);
            //设置缓存 只能网络加载
//            settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
            //设置操作文件
            settings.setAllowFileAccess(true);
            settings.setJavaScriptCanOpenWindowsAutomatically(true);
            settings.setSupportMultipleWindows(true);
            settings.setAppCacheEnabled(true);
            settings.setDomStorageEnabled(true);
            settings.setBlockNetworkImage(false);
            settings.setJavaScriptCanOpenWindowsAutomatically(true);
            if (!TextUtils.isEmpty(data)) {
                if(isHttp(data)){
                    settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
                    webView.setWebViewClient(new WebViewClient(){
                        @Override
                        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                                view.loadUrl( request.getUrl().toString());
                            } else {
                                view.loadUrl(request.toString());
                            }
                            return true;
                        }
                    });
                    webView.loadUrl(data);
                }else{
                    settings.setTextZoom(270);
                    String maxWidthImage = "<img style=\"width: 100%; height: auto;\"";
                    data=data.replaceAll("<img",maxWidthImage);
                    webView.loadDataWithBaseURL(null, data, "text/html", "UTF-8", null);
                }
            }
        }
    }

    public static boolean isHttp(String data) {
        if (!TextUtils.isEmpty(data)) {
            if (data.length() > 4 && data.substring(0, 4).contains("http")) {
                return true;
            }
        }
        return false;
    }
}

